package zhihui.mobile.ui.express;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import zhihui.mobile.bean.ilpay.PayResult;
import zhihui.mobile.bean.wx.WeixinPayInfo;
import zhihui.mobile.config.Config;

/**
 * Created by Administrator on 2017/9/18.
 */

public class ExpressPayUtils {
    private Activity context;
    private static final int SDK_PAY_FLAG = 1;
    private IWXAPI api;

    public ExpressPayUtils(Activity context) {
        this.context = context;
        api = WXAPIFactory.createWXAPI(context, Config.WX_APP_ID, true);
        api.registerApp(Config.WX_APP_ID);
    }

    private Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    PayResult payResult = new PayResult((String) msg.obj);
                    String resultStatus = payResult.getResultStatus();
                    if (TextUtils.equals(resultStatus, "9000")) {
                        com.zgm.utils.UiUtils.ToastInCenter(context, "支付成功", Toast.LENGTH_SHORT);
                        //跳转订单详情界面
//                        confirmPresenter.goOrderDetail();
//                        managerPresenter.goShopOrderDetail(ShopOrderManagerActivity.this, orderId, ShopOrderManagerActivity.this);
                        EventBus.getDefault().post(new BaseEventBus("finish_pay"));
                    } else {
                        if (TextUtils.equals(resultStatus, "8000")) {
                            com.zgm.utils.UiUtils.ToastInCenter(context, "支付结果确认中", Toast.LENGTH_SHORT);
//                            tv_buy.setText("支付确认中");
//                            tv_buy.setEnabled(false);
                        } else {

                            com.zgm.utils.UiUtils.ToastInCenter(context, "支付失败", Toast.LENGTH_SHORT);
//                            tv_buy.setText("重新支付");
//                            tv_buy.setEnabled(true);
                            EventBus.getDefault().post(new BaseEventBus("finish_pay"));
                        }
                    }
                    break;
                }
                default:
                    break;
            }
        }
    };

    public void ilPay(final String data) {
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                // 构造PayTask 对象
                PayTask alipay = new PayTask(context);
                // 调用支付接口，获取支付结果
                String result = alipay.pay(data, true);

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHander.sendMessage(msg);
            }
        };
        Thread thread = new Thread(payRunnable);
        thread.start();
    }


    public void weixinPay(WeixinPayInfo payInfo) {
        PayReq payRequest = new PayReq();
        payRequest.appId = Config.WX_APP_ID;
        payRequest.partnerId = payInfo.getPartnerid();
        payRequest.prepayId = payInfo.getPrepayid();
        payRequest.packageValue = payInfo.getPackages();
        payRequest.nonceStr = payInfo.getNoncestr();
        payRequest.timeStamp = payInfo.getTimestamp();
        payRequest.sign = payInfo.getSign();
        boolean result = api.sendReq(payRequest);
        if (!result) {
            com.zgm.utils.UiUtils.ToastInCenter(context, "请检查微信是否启动", Toast.LENGTH_SHORT);
        }
    }

}
